import * as Blockly from 'blockly';

export const registerCustomBlocks = (workspace: Blockly.WorkspaceSvg) => {
  // 立方体创建块
  Blockly.Blocks['create_cube'] = {
    init: function() {
      this.appendDummyInput()
          .appendField('Create cube');
      this.appendValueInput('SIZE')
          .setCheck('Number')
          .appendField('size');
      this.appendValueInput('COLOR')
          .setCheck('Colour')
          .appendField('color');
      this.setPreviousStatement(true, null);
      this.setNextStatement(true, null);
      this.setColour(230);
      this.setTooltip('Create a 3D cube with specified size and color');
    }
  };

  // 球体创建块
  Blockly.Blocks['create_sphere'] = {
    init: function() {
      this.appendDummyInput()
          .appendField('Create sphere');
      this.appendValueInput('RADIUS')
          .setCheck('Number')
          .appendField('radius');
      this.appendValueInput('COLOR')
          .setCheck('Colour')
          .appendField('color');
      this.setPreviousStatement(true, null);
      this.setNextStatement(true, null);
      this.setColour(230);
      this.setTooltip('Create a 3D sphere with specified radius and color');
    }
  };

  // 圆柱体创建块
  Blockly.Blocks['create_cylinder'] = {
    init: function() {
      this.appendDummyInput()
          .appendField('Create cylinder');
      this.appendValueInput('RADIUS')
          .setCheck('Number')
          .appendField('radius');
      this.appendValueInput('HEIGHT')
          .setCheck('Number')
          .appendField('height');
      this.appendValueInput('COLOR')
          .setCheck('Colour')
          .appendField('color');
      this.setPreviousStatement(true, null);
      this.setNextStatement(true, null);
      this.setColour(230);
      this.setTooltip('Create a 3D cylinder with specified dimensions and color');
    }
  };

  // 物体移动块
  Blockly.Blocks['move_object'] = {
    init: function() {
      this.appendDummyInput()
          .appendField('Move');
      this.appendValueInput('X')
          .setCheck('Number')
          .appendField('X');
      this.appendValueInput('Y')
          .setCheck('Number')
          .appendField('Y');
      this.appendValueInput('Z')
          .setCheck('Number')
          .appendField('Z');
      this.setPreviousStatement(true, null);
      this.setNextStatement(true, null);
      this.setColour(160);
      this.setTooltip('Move object by specified coordinates');
    }
  };

  // 物体旋转块
  Blockly.Blocks['rotate_object'] = {
    init: function() {
      this.appendDummyInput()
          .appendField('Rotate')
          .appendField(new Blockly.FieldDropdown([
            ['X', 'x'],
            ['Y', 'y'], 
            ['Z', 'z']
          ]), 'AXIS');
      this.appendValueInput('ANGLE')
          .setCheck('Number')
          .appendField('angle');
      this.setPreviousStatement(true, null);
      this.setNextStatement(true, null);
      this.setColour(160);
      this.setTooltip('Rotate object around specified axis');
    }
  };

  // 为每个块添加JavaScript代码生成器
  Blockly.JavaScript['create_cube'] = function(block) {
    const size = Blockly.JavaScript.valueToCode(block, 'SIZE', Blockly.JavaScript.ORDER_ATOMIC) || '1';
    const color = Blockly.JavaScript.valueToCode(block, 'COLOR', Blockly.JavaScript.ORDER_ATOMIC) || '0x00ff00';
    return `create3DObject('cube', { size: ${size}, color: ${color} });\n`;
  };

  Blockly.JavaScript['create_sphere'] = function(block) {
    const radius = Blockly.JavaScript.valueToCode(block, 'RADIUS', Blockly.JavaScript.ORDER_ATOMIC) || '1';
    const color = Blockly.JavaScript.valueToCode(block, 'COLOR', Blockly.JavaScript.ORDER_ATOMIC) || '0xff0000';
    return `create3DObject('sphere', { radius: ${radius}, color: ${color} });\n`;
  };

  Blockly.JavaScript['create_cylinder'] = function(block) {
    const radius = Blockly.JavaScript.valueToCode(block, 'RADIUS', Blockly.JavaScript.ORDER_ATOMIC) || '1';
    const height = Blockly.JavaScript.valueToCode(block, 'HEIGHT', Blockly.JavaScript.ORDER_ATOMIC) || '2';
    const color = Blockly.JavaScript.valueToCode(block, 'COLOR', Blockly.JavaScript.ORDER_ATOMIC) || '0x0000ff';
    return `create3DObject('cylinder', { radius: ${radius}, height: ${height}, color: ${color} });\n`;
  };

  Blockly.JavaScript['move_object'] = function(block) {
    const x = Blockly.JavaScript.valueToCode(block, 'X', Blockly.JavaScript.ORDER_ATOMIC) || '0';
    const y = Blockly.JavaScript.valueToCode(block, 'Y', Blockly.JavaScript.ORDER_ATOMIC) || '0';
    const z = Blockly.JavaScript.valueToCode(block, 'Z', Blockly.JavaScript.ORDER_ATOMIC) || '0';
    return `moveObject(currentObjectIndex, ${x}, ${y}, ${z});\n`;
  };

  Blockly.JavaScript['rotate_object'] = function(block) {
    const axis = block.getFieldValue('AXIS');
    const angle = Blockly.JavaScript.valueToCode(block, 'ANGLE', Blockly.JavaScript.ORDER_ATOMIC) || '0';
    return `rotateObject(currentObjectIndex, '${axis}', ${angle});\n`;
  };

  // 跟踪当前操作的物体索引
  Blockly.JavaScript['create_cube'] = function(block) {
    const size = Blockly.JavaScript.valueToCode(block, 'SIZE', Blockly.JavaScript.ORDER_ATOMIC) || '1';
    const color = Blockly.JavaScript.valueToCode(block, 'COLOR', Blockly.JavaScript.ORDER_ATOMIC) || '0x00ff00';
    return `currentObjectIndex = objects.length;\ncreate3DObject('cube', { size: ${size}, color: ${color} });\n`;
  };

  Blockly.JavaScript['create_sphere'] = function(block) {
    const radius = Blockly.JavaScript.valueToCode(block, 'RADIUS', Blockly.JavaScript.ORDER_ATOMIC) || '1';
    const color = Blockly.JavaScript.valueToCode(block, 'COLOR', Blockly.JavaScript.ORDER_ATOMIC) || '0xff0000';
    return `currentObjectIndex = objects.length;\ncreate3DObject('sphere', { radius: ${radius}, color: ${color} });\n`;
  };

  Blockly.JavaScript['create_cylinder'] = function(block) {
    const radius = Blockly.JavaScript.valueToCode(block, 'RADIUS', Blockly.JavaScript.ORDER_ATOMIC) || '1';
    const height = Blockly.JavaScript.valueToCode(block, 'HEIGHT', Blockly.JavaScript.ORDER_ATOMIC) || '2';
    const color = Blockly.JavaScript.valueToCode(block, 'COLOR', Blockly.JavaScript.ORDER_ATOMIC) || '0x0000ff';
    return `currentObjectIndex = objects.length;\ncreate3DObject('cylinder', { radius: ${radius}, height: ${height}, color: ${color} });\n`;
  };
};